<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
      xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link rel="stylesheet" href="/layui/css/layui.css">
  <link rel="stylesheet" href="/css/custom.form.css">
</head>
<body>
<div class="table_div">
  <div id="searchParam"  shiro:hasPermission="tGhkh:list">
    <div class="layui-form-item">
<!--        //入公海时间、入公海原因、客户来源-->
        <div class="layui-input-inline layui-form">
            <input type="text" name="date" class="layui-input" id="test-range0" placeholder="请选择入公海时间">
        </div>
        <div class="layui-input-inline layui-form" id="ssxsSCid" >
            <select id="rghyySC">
                <option value="">请选择入公海原因</option>
                <option value="首次导入">首次导入</option>
                <option value="自动流入">自动流入</option>
                <option value="主动放弃">主动放弃</option>
            </select>
        </div>
        <div class="layui-input-inline layui-form">
            <select id="khlySC">
                <option value="">请选择客户来源</option>
            </select>
        </div>
        <div class="layui-input-inline layui-form">
            <select id="xzghSC">
                <option value="">请选择公海</option>
            </select>
        </div>
      <div class="layui-input-inline ">
        <button class="layui-btn" onclick="search()"  id="search">查询</button>
          <button type="button" class="layui-btn" id="uploadFile-button" style="display:none">
              <i class="layui-icon">&#xe67c;</i>上传文件
          </button>
      </div>
    </div>

  </div>
  <table class="layui-table" id="showTable" lay-filter="showTable" ></table>
</div>
<div class="panel panel-default " id="operationlrgh" hidden>
    <form class="layui-form " action="" lay-filter="infogh" style="width: 700px;margin-top: 10px">
        <div class="layui-form-item">
            <label class="layui-form-label">公海</label>
            <div class="layui-input-block">
                <select id="lrghSCF" name="gh">
                    <option value="">请选择公海</option>
                </select>
            </div>
        </div>
    </form>
</div>
<div class="panel panel-default " id="operationlrgh2" hidden>
    <form class="layui-form " action="" lay-filter="infogh" style="width: 700px;margin-top: 10px">
        <div class="layui-form-item">
            <label class="layui-form-label">变更负责人</label>
            <div class="layui-input-block">
                <select id="fzrFC" name="fzr">
                    <option value="">请选择负责人</option>
                </select>
            </div>
        </div>
    </form>
</div>
<script type="text/html" id="toolbar">
  <div class="layui-btn-container">
<!--    <button class="layui-btn layui-btn-sm" lay-event="add"  shiro:hasPermission="tGhkh:add">添加</button>-->
      <button class="layui-btn layui-btn-sm" lay-event="fp" shiro:hasPermission="tGhkh:fp">分配</button>
      <button class="layui-btn layui-btn-sm" lay-event="lq" shiro:hasPermission="tGhkh:lq">领取</button>
    <button class="layui-btn layui-btn-sm" lay-event="batchDeleted" shiro:hasPermission="tGhkh:delete">删除</button>
      <button style="float:right" class="layui-btn layui-btn-sm" lay-event="pldr" shiro:hasPermission="tGhkh:pldr"><i class="layui-icon">&#xe67c;</i>批量导入</button>
      <button style="float:right" class="layui-btn layui-btn-sm" lay-event="xzmb"shiro:hasPermission="tGhkh:xzmb">下载模板</button>

  </div>


  <div class="panel panel-default " id="zhuanyidata" hidden>
      <form class="layui-form " action="" lay-filter="infogh" style="width: 700px;margin-top: 10px">
          <div class="layui-form-item">
              <label class="layui-form-label">变更负责人</label>
              <div class="layui-input-block">
                  <select id="usernameFC" name="usernameFC">
                      <option value="">请选择负责人</option>
                  </select>
              </div>
          </div>
      </form>
  </div>

<!--  <div id="zhuanyidata" hidden>-->
<!--      <div class="panel panel-default operation2" >-->
<!--          <div class="panel-heading title"></div>-->
<!--          <div class="layui-card-body">-->
<!--              <form class="layui-form " action="" lay-filter="info" style="width: 700px;margin-top: 10px">-->
<!--                  <input name="id" hidden/>-->
<!--                  <div class="layui-form-item">-->
<!--                      <label class="layui-form-label">变更负责人</label>-->
<!--                      <div class="layui-input-inline layui-form">-->
<!--                          <select id="usernameFC" name="usernameFC">-->
<!--                              <option value="">请选择负责人</option>-->
<!--                          </select>-->
<!--                      </div>-->
<!--                  </div>-->
<!--                  <div class="layui-form-item layui-hide">-->
<!--                      <div class="layui-input-block">-->
<!--                          <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit2" id="zysubmit">确定</button>-->
<!--                          <button  class="layui-btn layui-btn-primary" id="btn_cancelzhuanyi">返回</button>-->
<!--                      </div>-->
<!--                  </div>-->
<!--              </form>-->
<!--          </div>-->
<!--      </div>-->
<!--  </div>-->

</script>
<script type="text/html" id="tool">
<!--  <a class="layui-btn layui-btn-xs" lay-event="edit" shiro:hasPermission="tGhkh:update">编辑</a>-->
<!--  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" shiro:hasPermission="tGhkh:delete">删除</a>-->
<a class="layui-btn layui-btn-xs" lay-event="info" >查看</a>
</script>

</body>
</html>
<script src="/layui/layui.all.js"></script>
<script src="/js/core.util.js"></script>
<script src="/js/jquery.js"></script>
<script src="/js/common.js"></script>
<script src="/js/jquery.fileDownload.js"></script>
<script>
  //获取token
  var token = CoreUtil.getData("access_token");
  //地址栏转义token中的#号
  var tokenQuery = token.replace("#", "%23");
  var tableIns1;
  var table = layui.table;
  var form = layui.form;
  var layer = layui.layer;
  var laydate = layui.laydate;
  var upload = layui.upload;
  var ghmcVo = "";
  info();
  var isycVo;
  layui.use(['table', 'layer', 'laydate','upload'], function () {
      laydate.render({
          elem: '#test-range0' ,//指定元素
          range: true,
      });
      //执行实例
      var uploadInst = upload.render({
          elem: '#uploadFile-button' //绑定元素
          ,url: '/wd/tGhkh/upload?authorization='+tokenQuery //上传接口
          ,accept: 'file' //允许上传的文件类型
          ,acceptMime: ".zip,.rar,.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx"
          ,exts: "zip|rar|pdf|doc|docx|xls|xlsx|ppt|pptx"
          ,before: function(obj) {
              var lrghSCF = $.trim($("#lrghSCF").val());//Id
              this.data = {
                  id: lrghSCF
              }
              layer.msg("正在导入文件中", {shade:0.4, icon:16, time:1000});
          }
          ,done: function(res){
              debugger
              var data =  res.data;
              if (res.code == 0) {
                  if (!!data){
                      alert(data+" 异常数据未上传成功！")
                  }
                  layer.msg("上传成功", {shade:0.4, icon:16, time:1000});
                  search();
              }else{
                  layer.msg(res.msg, {shade:0.4, icon:16, time:1000});
              }
          }
          ,error: function(res){
              //请求异常回调
              layer.msg("请求异常回调", {shade:0.4, icon:16, time:1000});
          }
      });

    //表头工具
    table.on('toolbar(showTable)', function(obj){
      switch(obj.event){
        case 'batchDeleted':
          var checkStatus = table.checkStatus(obj.config.id);
          var data = checkStatus.data;
          if(data.length==0){
            layer.msg("请选择要批量删除的列");
          }else {
            var ids = [];
            $(data).each(function (index,item) {
              ids.push(item.id);
            });
            tipDialog(ids);
          }
          break;
        case 'lq' : //领取
          var checkStatus = table.checkStatus(obj.config.id);
          var data = checkStatus.data;
          if(data.length==0){
              layer.msg("请选择要批量领取的列");
          }else {
              var ids = [];
              $(data).each(function (index, item) {
                  ids.push(item.id);
              });
          }
          lingqulog(ids);
          break;
        case 'fp' : //分配
          var checkStatus = table.checkStatus(obj.config.id);
          var data = checkStatus.data;
          if(data.length==0){
              layer.msg("请选择要批量删除的列");
          }else {
              var ids = [];
              $(data).each(function (index, item) {
                  ids.push(item.id);
              });
          }
          zhuanyilog(ids,"分配");
          break;
        case 'pldr' :
          lrghlog(ids);
          break;
        case 'xzmb' :
          $.fileDownload("/sysGenerator/model?authorization=" + tokenQuery,{httpMethod: 'get'})
          break;
      };
    });
      table.on('tool(showTable)', function (obj) {
          let data = obj.data;
          switch (obj.event) {
              case 'info':   //查看
                  window.open("/index/tGhkh/ghkhedit/"+data.id);
                  break;
          }
      });
      //双击
      table.on('rowDouble(showTable)',function(obj){
          let data = obj.data;
          //详情
          window.open("/index/tGhkh/ghkhedit/"+data.id);
      });
      var lrghlog=function (ids) {
          layer.open({
              type: 1,
              skin: 'layui-layer-molv',
              title: "选择公海",
              area: ['70%', '80%'],
              shade: 0,
              shadeClose: true,
              content: jQuery("#operationlrgh"),
              btn: ['立即提交','关闭'],
              end:function (res) {$("#operationlrgh").css("display",'none');},
              yes: function(index, layero){
                  $("#uploadFile-button").click();
                  search();
                  layer.close(index); //如果设定了yes回调，需进行手工关闭
              }
          });
      };
      var lingqulog = function (ids) {
          CoreUtil.sendPost("/tGhkh/lingqu", ids, function (res) {
              var code = res.code;
              //同步数据失败
              if (code!=0){
                  layer.msg(res.msg,);
              }
              search();
          });
      }
      var zhuanyilog=function (ids,title){
          idsData = ids;
          layer.open({
              type: 1,
              skin: 'layui-layer-molv',
              title: title,
              area: ['70%', '80%'],
              shade: 0,
              shadeClose: false,
              content: jQuery("#operationlrgh2"),
              btn: ['立即提交','关闭'],
              end:function (res) {$("#operationlrgh2").css("display",'none');},
              yes: function (index) {
                  var usernameFC = $.trim($("#fzrFC").val());//Id
                  if (usernameFC != undefined && usernameFC != ''){
                      var params = {
                          username: usernameFC,
                          ids: ids
                      }
                      CoreUtil.sendPost("/tGhkh/fenpei", params, function (res) {
                          
                          var code = res.code;
                          //同步数据失败
                          if (code!=0){
                              layer.msg(res.msg,{time:1000});
                          }
                          search();
                      });
                  }
                  layer.close(index);
              }
          });
      }
    //删除
    var tipDialog=function (ids) {
      layer.open({
        content: "确定要删除么?",
        yes: function(index, layero){
          layer.close(index); //如果设定了yes回调，需进行手工关闭
          CoreUtil.sendDelete("/tGhkh/delete",ids,function (res) {
            layer.msg(res.msg, {time:1000},function () {
              search();
            });
          });
        }
      });
    };
    //监听保存
    form.on('submit(submit)', function(data){
      if(data.field.id===undefined || data.field.id===null || data.field.id===""){
        CoreUtil.sendPost("/tGhkh/add",data.field,function (res) {
          $(".table_div").show();
          $(".operation").hide();
          search();
        });
      }else {
        CoreUtil.sendPut("/tGhkh/update",data.field,function (res) {
          $(".table_div").show();
          $(".operation").hide();
          search();
        });
      }
      return false;
    });
  });
  function tableIns() {
      tableIns1 = table.render({
          elem: '#showTable'
          , contentType: 'application/json'
          , headers: {"authorization": token}
          , page: true //开启分页
          , url: '/tGhkh/listByPage' //数据接口
          , method: 'POST'
          , limits: ['10', '20', '50','100', '300', '500', '1000']//分页每页显示数据条数（页面选择
          , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
              return {
                  "code": res.code, //解析接口状态
                  "msg": res.msg, //解析提示文本
                  "count": CoreUtil.isEmpty(res.data) ? 0 : res.data.total, //解析数据长度
                  "data": CoreUtil.isEmpty(res.data) ? null : res.data.records //解析数据列表
              }
          }
          , cols: [
              [
                  {type: 'checkbox', fixed: 'left'},
                  // {title: "序号",type: "numbers"},
                  // {field: 'id',title: "id",width: 220,fixed: "left" },
                  {field: 'khname', title: '客户名称',event: 'khname', sort: true,width: 220 ,
                     style: 'color: #003CFFFF; cursor:pointer',align: 'center',fixed: "left"},
                  {field: 'lxr', title: '联系人', width: 100 ,sort: true, align: 'center'},
                  {field: 'lxdh', title: '联系电话', width: 120 ,templet: function (item) {
                          if (item.lxdh.length == 11 && isycVo == 1) { //是否隐藏
                              return item.lxdh.replace(item.lxdh.substring(3, 7), "****");
                          } else if (item.lxdh.length > 11 && isycVo == 1) { //是否隐藏
                              return item.lxdh.replace(item.lxdh.substring(3, 9), "****");
                          } else {
                              return item.lxdh;
                          }
                      }, sort: true, align: 'center'},
                  {field: 'ssxs', title: '所属销售', width: 100 ,sort: true, align: 'center'},
                  {field: 'khjb', title: '意向度', width: 90 ,sort: true, align: 'center'},
                  {field: 'qdcs', title: '去电次数',width: 90 , sort: true, align: 'center'},
                  // {field: 'gjjl', title: '跟进记录',width: 110 , sort: true, align: 'center'},
                  {field: 'zhgjtime', title: '最后跟进时间',width: 220 , sort: true, align: 'center'},
                  {field: 'xclxtime', title: '下次联系时间',width: 220 , sort: true, align: 'center'},
                  {field: 'rktime', title: '入公海时间',width: 220 , sort: true, align: 'center'},
                  {field: 'khly', title: '客户来源',width: 100 , sort: true, align: 'center'},
                  {field: 'rghyy', title: '入公海原因', width: 100 ,sort: true, align: 'center'},
                  {field: 'khdz', title: '客户地址', width: 220 , sort: true,align: 'center'},
                  {field: 'khhy', title: '客户行业', width: 220 , sort: true,align: 'center'},
                  {width: 120, toolbar: "#tool", title: '操作',fixed: "right"}
              ]
          ]
          , toolbar: '#toolbar'
      });

  }
 function info() {
      CoreUtil.sendGet("/tGhkh/infoData", null, function (res) {
          var code = res.code;
          var data = res.data;
          //同步数据失败
          if (code==0){
              var fzrFC = $("#fzrFC");
              $.each(data.username, function (index, item) {
                  fzrFC.append("<option value=" + item + ">" + item + "</option>");
              });
              //客户来源
              var khlySC = $("#khlySC");
              $.each(data.khly, function (index, item) {
                  khlySC.append("<option value=" + item + ">" + item + "</option>");
              });
              //公海名称
              var lrghSCF = $("#lrghSCF");
              var xzghSC = $("#xzghSC");
              $.each(data.gh, function (index, item) {
                  if (!!item) {
                      lrghSCF.append("<option value=" + item.ghmc +";"+ item.id + ">" + item.ghmc + "</option>");
                      xzghSC.append("<option value=" + item.ghmc + ";"+ item.id + ">" + item.ghmc + "</option>");
                  }
              });
              isycVo = data.isyc;
              form.render("select");
          }
          CoreUtil.sendGet("/sys/home/", null, function (res) {
              userInfo = res.data.userInfo;
              userRoles = res.data.sysRole;
              userDepts = res.data.dept;
              userSysUsers = res.data.users;
              //加载表格
              tableIns();
          });
      });
  }


  //执行查询
  function search() {
      var split0 =  $('#test-range0').val().replace(/ - /g,' 至 ').split(' 至 ');
      //这里以搜索为例
    tableIns1.reload({
      where: { //设定异步数据接口的额外参数，任意设
          rghyy: $("#rghyySC").val(),
          ssgh: $("#xzghSC").val(),//所属公海
          khly: $("#khlySC").val(),
          rktimeS: split0[0],
          rktimeE: split0[1]
      }
      , page: {
        curr: 1 //重新从第 1 页开始
      }
    });
  };
</script>